package com.cyl.XmlDemo.web;

import java.security.Principal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cyl.XmlDemo.entity.DMsg;
import com.cyl.XmlDemo.entity.DataResult;
import com.cyl.XmlDemo.service.MsgService;
import com.cyl.XmlDemo.service.RoomService;

/**
 * 页面信息控制器
 * @author tyuio
 *
 */
@Controller
@RequestMapping("/msg")
public class MsgController {
	
	private final Logger log = LoggerFactory.getLogger(MsgController.class);

	@Autowired
	private MsgService msgService;
	
	@Autowired
	private RoomService roomService;
	
	@RequestMapping(value="/taste",method=RequestMethod.GET)
	@ResponseBody
	public DataResult Taste(DataResult dr){
		log.debug("/msg/taste");
		dr.setData(roomService.create());
		dr.setCode("success");
		return dr;
	}
	
	@MessageMapping("/topic/transfer-msg")
	public void transfer(DMsg dmsg,Principal principal){
		log.debug(principal.getName()+" 信息中转: "+dmsg.getData());
		dmsg.setSender(principal.getName());
//		msgService.transferMsg(dmsg);
		msgService.sendToUser(dmsg);
	}
	
	@MessageMapping("/topic/talk-msg-transfer")
	public void talkTransfer(DMsg dmsg,Principal principal){
		log.debug("聊天信息发送"+dmsg.getData());
		dmsg.setSender(principal.getName());
		msgService.transferTalkMsg(dmsg);
	}
}
